﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LuDo.PitEditor
{
    class RichFormat
    {
        public enum FormatType { Bold, Underline, Italic, Strikethrough, Superscript, Code, List, Link };
        public FormatType Type { get; set; }
        public int StartPoint { get; set; }
        public int Length { get; set; }
        public string URL { get; set; }
        public string TargetLink { get; set; }
        public enum ListTypes { None, Unsorted, Sorted, Alphabetic };
        public ListTypes ListType { get; set; }

        /// <summary>
        /// The last position of a formatted character, next one will be outside formatting.
        /// </summary>
        public int EndPoint
        {
            get
            {
                return StartPoint + Length - 1;
            }
        }

        public RichFormat(FormatType type, int start, int length)
        {
            this.Type = type;
            this.StartPoint = start;
            this.Length = length;
            this.ListType = ListTypes.None;
        }

        //Confronti con una posizione
        internal bool IsBefore(int position)
        {
            //bool IsTrue = IsEnter ? this.StartPoint + this.Length < position + 1 : this.StartPoint + this.Length < position;
            bool IsTrue = this.StartPoint + this.Length <= position;
            return IsTrue;
        }
        internal bool IsFirstPosition(int position)
        {
            bool IsTrue = position == this.StartPoint;
            return IsTrue;
        }
        internal bool IsAround(int position)
        {
            bool IsTrue = this.StartPoint <= position && position <= this.StartPoint + this.Length - 1;
            return IsTrue;
        }
        internal bool IsNextPosition(int position)
        {
            //bool IsTrue = IsEnter ? position < this.StartPoint + this.Length : position == this.StartPoint + this.Length;
            bool IsTrue = position == this.StartPoint + this.Length;
            return IsTrue;
        }
        internal bool IsAfter(int position)
        {
            bool IsTrue = position < this.StartPoint;
            return IsTrue;
        }

        //Confronti con una selezione
        internal bool IsEqualToSelection(int start, int length)
        {
            bool IsTrue = this.StartPoint == start && this.Length == length;
            return IsTrue;
        }
        internal bool IsInsideSelection(int start, int length)
        {
            int end = start + length - 1;
            bool IsTrue = start <= this.StartPoint && this.EndPoint <= end && !IsEqualToSelection(start, length);
            return IsTrue;
        }
        internal bool IsOutsideSelection(int start, int length)
        {
            int end = start + length - 1;
            bool IsTrue = this.StartPoint <= start && end <= this.EndPoint && !IsEqualToSelection(start, length);
            return IsTrue;
        }
        internal bool IsLeftContainedInSelection(int start, int length)
        {
            int end = start + length - 1;
            bool IsTrue = start < this.StartPoint && this.StartPoint < end && end < this.EndPoint;
            return IsTrue;
        }
        internal bool IsRightContainedInSelection(int start, int length)
        {
            int end = start + length - 1;
            bool IsTrue = this.StartPoint < start && start < this.EndPoint && this.EndPoint < end;
            return IsTrue;
        }
        internal bool IsOverlyingSelection(int start, int length)
        {
            bool eq = IsEqualToSelection(start, length);
            bool inside = IsInsideSelection(start, length);
            bool outside = IsOutsideSelection(start, length);
            bool left = IsLeftContainedInSelection(start, length);
            bool right = IsRightContainedInSelection(start, length);
            bool IsTrue = eq || inside || outside || left || right;
            return IsTrue;
        }
    }
}
